package com.kedacom.freedomLabel.view.activity.item;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.databinding.ActivityItemIDControlBinding;
import com.kedacom.freedomLabel.model.DataControlResult;
import com.kedacom.freedomLabel.model.bean.Item;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.dao.ItemDao;
import com.kedacom.freedomLabel.view.fragment.ListDataFragment;
import com.kedacom.freedomLabel.viewmodel.item.IDControlViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.fast.validation.RegexTemplate;
import com.kedacom.lego.message.LegoMessageManager;

import java.util.regex.Pattern;


@ContentView(R.layout.activity_item_i_d_control)
@ViewModel(IDControlViewModel.class)
public class IDControlActivity extends BaseActivity<ActivityItemIDControlBinding, IDControlViewModel> {

    Item mSelectedItem = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        closeEditLayout();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        ItemDao itemDao = AppDatabase.getInstance().itemDao();
        ListDataFragment<Item> fragment = ListDataFragment.newInstance(itemDao);
        transaction.add(mBinding.recyclerContainer.getId(), fragment);
        transaction.commit();
        mBinding.radioGroup.check(R.id.radio_id1);
    }

    @OnMessage(value = Options.MSG_SELECT_ITEM, discard = true)
    public void receiveItemClickMessage(Item item) {
        closeEditLayout();
        mSelectedItem = item;
        mBinding.evName.setText(item.value);
        mBinding.evNumber.setText(item.number);
        mBinding.evRemark.setText(item.remark);
        if (item.value != null) {
            mBinding.evName.setSelection(item.value.length());
        }
        if (item.number != null) {
            mBinding.evNumber.setSelection(item.number.length());
        }
        if (item.remark != null) {
            mBinding.evRemark.setSelection(item.remark.length());
        }
    }

    /**
     * 接收数据更新结果通知
     *
     * @param result 结果
     */
    @OnMessage(value = Options.MSG_SAVE_ITEM_RESULT, discard = true)
    public void receiveDataSaveResult(DataControlResult<Item> result) {
        if (result.result) {
            closeEditLayout();
            mSelectedItem = result.data;
        }
    }

    /**
     * 接收数据删除结果通知
     *
     * @param result 结果
     */
    @OnMessage(value = Options.MSG_DELETE_ITEM_RESULT, discard = true)
    public void receiveDataDeleteResult(DataControlResult<Item> result) {
        if (result.result) {
            mSelectedItem = null;
            clear();
        }
    }

    /**
     * 接收新增数据结果通知
     *
     * @param result 结果
     */
    @OnMessage(value = Options.MSG_ADD_ITEM_RESULT, discard = true)
    public void addItemComplete(DataControlResult<Item> result) {
        if (result.result) {
            closeEditLayout();
        }
    }

    @OnMessage(value = Options.MSG_RECOVERY_ITEM_DETAIL, discard = true)
    public void recoveryDetailLayout() {
        clear();
        closeEditLayout();
    }

    // 保存
    public void onSaveIDViewClick(View view) {
        beep();
        String name = mBinding.evName.getText().toString();
        String number = mBinding.evNumber.getText().toString();
        if (!Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(name).matches()) {
            showToast(getString(R.string.validation_empty_name), TOAST_WARNING);
            return;
        }
        if (!Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(number).matches()) {
            showToast(getString(R.string.validation_empty_number), TOAST_WARNING);
            return;
        }
        Object tag = mBinding.save.getTag();
        if (tag != null) {
            mViewModel.updateID(mSelectedItem);
        } else {
            mViewModel.saveID();
        }
    }

    // 删除
    public void onDeleteIDViewClick(View view) {
        beep();
        showDialog("是否删除", "删除", (dialog, which) -> {
            LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_DELETE_ITEM);
            dialog.dismiss();
        });
    }

    // 增加
    public void onAddIDViewClick(View view) {
        beep();
        openEditLayout();
        clear();
    }

    private void clear() {
        mBinding.save.setTag(null);
        mBinding.evName.setText(null);
        mBinding.evNumber.setText(null);
        mBinding.evRemark.setText(null);
        mBinding.evName.setSelection(0);
        mBinding.evNumber.setSelection(0);
        mBinding.evRemark.setSelection(0);
    }

    // 编辑
    public void onEditIDViewClick(View view) {
        beep();
        if (mSelectedItem != null) {
            openEditLayout();
            // 标识编辑
            mBinding.save.setTag(mSelectedItem.uuid);
        }
    }

    // 清除
    public void onClearIDViewClick(View view) {
        beep();
        mBinding.evName.setText(null);
        mBinding.evNumber.setText(null);
        mBinding.evRemark.setText(null);
        mBinding.evName.setSelection(0);
        mBinding.evNumber.setSelection(0);
        mBinding.evRemark.setSelection(0);
    }

    // 返回
    public void onCancelIDViewClick(View view) {
        beep();
        closeEditLayout();
        // 标识新增
        mBinding.save.setTag(null);
    }

    // 退出
    public void onBackClick(View view) {
        beep();
        finish();
    }

    private void closeEditLayout() {
        mBinding.evName.setEnabled(false);
        mBinding.evNumber.setEnabled(false);
        mBinding.evRemark.setEnabled(false);
        mBinding.controlAddLayout.setVisibility(View.VISIBLE);
        mBinding.controlSaveLayout.setVisibility(View.GONE);
    }

    private void openEditLayout() {
        mBinding.evName.setEnabled(true);
        mBinding.evNumber.setEnabled(true);
        mBinding.evRemark.setEnabled(true);
        mBinding.controlAddLayout.setVisibility(View.GONE);
        mBinding.controlSaveLayout.setVisibility(View.VISIBLE);
    }
}